package com.ymatou.iapi.livebuyer.testcase;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.ymatou.iapi.livebuyer.parameter.ProcessStockChangeBean;
import com.ymatou.iapi.livebuyer.parameter.QuerySecKillProductsByTypeBean;
import com.ymatou.iapi.livebuyer.service.ProcessStockChangeCall;
import com.ymatou.iapi.livebuyer.service.QuerySecKillProductsByTypeCall;
import com.ymatou.iapi.liveseller.parameter.CreateLiveActivityBean;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.liveseller.parameter.ModifyLiveActivityBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.RunTestsV2;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
/**
*直播会场秒杀商品
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_QuerySecKillProductsByType{
	private static QuerySecKillProductsByTypeBean queryseckillproductsbytypeBean;
	private static QuerySecKillProductsByTypeCall queryseckillproductsbytypeCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static int sellerId2 = Integer.parseInt(EnvSetup.getData("SellerId2"));
	LiveRoomWapper LiveRoomWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("直播会场秒杀商品");
		
	}

	@Before
	public void caseUp() {
		queryseckillproductsbytypeBean=new QuerySecKillProductsByTypeBean();
		queryseckillproductsbytypeCall=new QuerySecKillProductsByTypeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(queryseckillproductsbytypeCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QuerySecKillProductsByType_001() {
		Logger.start(true,"直播会场秒杀商品查询成功type1进行中");
		try {
			LiveRoomWapper=new LiveRoomWapper();
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			
			Date starttime = new Date(now.getTime() + 4* 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 5d, 6, starttime);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), sellerId);
			Integer liveacctivityid = LiveRoomWapper.selectLiveactivityByLiveId(createliveactivityBean.getLiveActivity().getLiveId()).get(0).getLiveActivityId();
			
			
			Thread.sleep(1000);
			
			queryseckillproductsbytypeBean.setLiveIds(Arrays.asList(createlive.getId()));
			queryseckillproductsbytypeBean.setType(1);
			queryseckillproductsbytypeCall.setData(queryseckillproductsbytypeBean);
			queryseckillproductsbytypeCall.callService();			
			Logger.verifyEquals("200", queryseckillproductsbytypeCall.getString("code"), "验证code");
			JsonArray  jas = queryseckillproductsbytypeCall.getJsonObject().get("data").getAsJsonArray();
			Logger.verifyEquals(1, jas.size(), "验证结果数量");
			Logger.verifyEquals(productId, jas.get(0).getAsJsonObject().get("productId").getAsString(), "验证productId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QuerySecKillProductsByType_002() {
		//现在的未来会查询5天内的
		Logger.start(true,"直播会场秒杀商品查询成功type2未开始");
		try {
			LiveRoomWapper=new LiveRoomWapper();
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 4*24*60 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 4*24*60 * 60 * 1000+60 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 5d, 6, starttime);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), sellerId);
			Integer liveacctivityid = LiveRoomWapper.selectLiveactivityByLiveId(createliveactivityBean.getLiveActivity().getLiveId()).get(0).getLiveActivityId();
			
			queryseckillproductsbytypeBean.setLiveIds(Arrays.asList(createlive.getId()));
			queryseckillproductsbytypeBean.setType(2);
			queryseckillproductsbytypeCall.setData(queryseckillproductsbytypeBean);
			queryseckillproductsbytypeCall.callService();			
			Logger.verifyEquals("200", queryseckillproductsbytypeCall.getString("code"), "验证code");
			JsonArray jas = queryseckillproductsbytypeCall.getJsonObject().get("data").getAsJsonArray();
			Logger.verifyEquals(1, jas.size(), "验证结果数量");
			Logger.verifyEquals(productId, jas.get(0).getAsJsonObject().get("productId").getAsString(), "验证productId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	//TODO 确认已结束条件
	public void Tc_QuerySecKillProductsByType_003() {
		Logger.start(true,"直播会场秒杀商品查询成功type3已结束");
		try {
			LiveRoomWapper=new LiveRoomWapper();
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 5 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 5d, 6, starttime);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), sellerId);
			Integer liveacctivityid = LiveRoomWapper.selectLiveactivityByLiveId(createliveactivityBean.getLiveActivity().getLiveId()).get(0).getLiveActivityId();
			LiveRoomWapper.updateLiveactivityStatusToEndByProduct(productId);
			Thread.sleep(5000);
			queryseckillproductsbytypeBean.setLiveIds(Arrays.asList(createlive.getId()));
			queryseckillproductsbytypeBean.setType(3);
			queryseckillproductsbytypeCall.setData(queryseckillproductsbytypeBean);
			queryseckillproductsbytypeCall.callService();			
			Logger.verifyEquals("200", queryseckillproductsbytypeCall.getString("code"), "验证code");
			JsonArray jas = queryseckillproductsbytypeCall.getJsonObject().get("data").getAsJsonArray();
			Logger.verifyEquals(1, jas.size(), "验证结果数量");
			Logger.verifyEquals(productId, jas.get(0).getAsJsonObject().get("productId").getAsString(), "验证productId");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QuerySecKillProductsByType_004() {
		Logger.start(true,"查询多个直播间编号成功");
		try {
			LiveRoomWapper=new LiveRoomWapper();
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 100 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 5d, 6, starttime);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), sellerId);
			Integer liveacctivityid = LiveRoomWapper.selectLiveactivityByLiveId(createliveactivityBean.getLiveActivity().getLiveId()).get(0).getLiveActivityId();
			queryseckillproductsbytypeBean.setLiveIds(Arrays.asList(createlive.getId()));
			
			AddSellerProductRequest addpro2 = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId2);
			String productId2 = ProductManagerCallServiceV2.addNewProduct(addpro2).get("Data").getAsJsonObject().get("ProductId").getAsString();
			CreateSellerLiveBean clive2 = LiveRoomCallService.initCreatesellerliveBean(sellerId2);
			Date now2 = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime2 = new Date(now2.getTime() + 100 * 1000);
			Date endtime2 = new Date(now2.getTime() + 200 * 60 * 1000);
			clive2.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime2, YMTDateUtil.YMDHMS));
			LiveRoom createlive2 = LiveRoomCallService.CreateSellerLive(clive2);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive2.getId(), sellerId2, productId2);
			Logger.comment("查询商品规格");
			List<String> cids2 = CIWapper.selectCatalogsByProductId(productId2).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			CreateLiveActivityBean createliveactivityBean2 = LiveRoomCallService.initCreateLiveActivityBean(createlive2.getId(), cids2.get(0), 5d, 6, starttime2);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean2);
			LiveRoomCallService.SellerLiveOnline(createlive2.getId(), sellerId2);
			Integer liveacctivityid2 = LiveRoomWapper.selectLiveactivityByLiveId(createliveactivityBean2.getLiveActivity().getLiveId()).get(0).getLiveActivityId();
			
			queryseckillproductsbytypeBean.setLiveIds(Arrays.asList(createlive.getId(),createlive2.getId()));
			queryseckillproductsbytypeBean.setType(2);
			queryseckillproductsbytypeCall.setData(queryseckillproductsbytypeBean);
			queryseckillproductsbytypeCall.callService();		
			JsonArray jas = queryseckillproductsbytypeCall.getJsonObject().get("data").getAsJsonArray();
			Logger.verifyEquals(2, jas.size(), "验证结果数量");
			Logger.verifyEquals(productId, jas.get(0).getAsJsonObject().get("productId").getAsString(), "验证productId");
			Logger.verifyEquals(productId2, jas.get(1).getAsJsonObject().get("productId").getAsString(), "验证productId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QuerySecKillProductsByType_005() {
		Logger.start(true,"不输入type失败");
		try {
			
			queryseckillproductsbytypeBean.setLiveIds(Arrays.asList(1));
			queryseckillproductsbytypeCall.setData(queryseckillproductsbytypeBean);
			queryseckillproductsbytypeCall.callService();			
			Logger.verifyEquals("100", queryseckillproductsbytypeCall.getString("code"), "code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QuerySecKillProductsByType_006() {
		Logger.start(true,"输入错误type失败");
		try {
			queryseckillproductsbytypeBean.setType(-1);
			queryseckillproductsbytypeBean.setLiveIds(Arrays.asList(1));
			queryseckillproductsbytypeCall.setData(queryseckillproductsbytypeBean);
			queryseckillproductsbytypeCall.callService();			
			Logger.verifyEquals("100", queryseckillproductsbytypeCall.getString("code"), "code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QuerySecKillProductsByType_007() {
		Logger.start(true,"不输入LiveId失败");
		try {
			queryseckillproductsbytypeBean.setType(1);
//			queryseckillproductsbytypeBean.setLiveIds(Arrays.asList(1));
			queryseckillproductsbytypeCall.setData(queryseckillproductsbytypeBean);
			queryseckillproductsbytypeCall.callService();			
			Logger.verifyEquals("100", queryseckillproductsbytypeCall.getString("code"), "code");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QuerySecKillProductsByType_008() {
		Logger.start(true,"新增参数EndTime");
		try {
			LiveRoomWapper=new LiveRoomWapper();
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 5* 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 5d, 6, starttime);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), sellerId);
			Integer liveacctivityid = LiveRoomWapper.selectLiveactivityByLiveId(createliveactivityBean.getLiveActivity().getLiveId()).get(0).getLiveActivityId();
			
			
			Thread.sleep(3000);
			queryseckillproductsbytypeBean.setLiveIds(Arrays.asList(createlive.getId()));
			queryseckillproductsbytypeBean.setType(1);
			queryseckillproductsbytypeCall.setData(queryseckillproductsbytypeBean);
			queryseckillproductsbytypeCall.callService();			
			Logger.verifyEquals("200", queryseckillproductsbytypeCall.getString("code"), "验证code");
			JsonArray jas = queryseckillproductsbytypeCall.getJsonObject().get("data").getAsJsonArray();
			Logger.verifyEquals(1, jas.size(), "验证结果数量");
			Logger.verifyEquals(productId, jas.get(0).getAsJsonObject().get("productId").getAsString(), "验证productId");
			Logger.verifyNotNull(jas.get(0).getAsJsonObject().get("endTime").getAsString(), "验证productId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}